package com.zy.datareceiver.binsess.stomp.controller;

import com.zy.datareceiver.binsess.monitoring.service.RealMonitorService;
import com.zy.datareceiver.core.cast.CacheDict;
import com.zy.datareceiver.core.cast.ConCast;
import com.zy.datareceiver.core.common.ServerResponse;
import com.zy.datareceiver.core.utils.JsonUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.MessageMapping;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class StompController {
    private static final Logger log = LoggerFactory.getLogger(StompController.class);
    //spring提供的推送方式
    @Autowired
    private SimpMessagingTemplate simpMessagingTemplate;
    @Autowired
    private RealMonitorService realMonitorService;

    @MessageMapping("/ack")
    public void sendPublicMessage(Integer pointId) {
        log.info("订阅监测点 {}", pointId);
        ServerResponse response = null;
        if (pointId == -1){
            simpMessagingTemplate.convertAndSend(ConCast.StompDestination.INDEX_LARGE_SCREEN_PATH, "OK");
        }
        if (pointId == 0) {
            ServerResponse response1 = realMonitorService.getMonitoringIndexCollectData();
            simpMessagingTemplate.convertAndSend(ConCast.StompDestination.INDEX_LARGE_SCREEN_PATH, JsonUtil.obj2String(response1));
            return ;
        }
        if (CacheDict.getAllPointId().contains(pointId)) {
            ServerResponse response1 = realMonitorService.getMonitoringCollectData(pointId);
            simpMessagingTemplate.convertAndSend(ConCast.StompDestination.TOPIC_PREFIX+pointId, JsonUtil.obj2String(response1));
        }
    }
}
